package semantic.model;

import java.util.LinkedList;
import java.util.List;

public class PropertyCallNode {

	private String path;
	
	private boolean hasTimeExpression;
	
	private boolean isMethod;
	
	private List<ExpressionNode> parameterList;
	
	private AccessorTypeEnum accessor;
	
	public PropertyCallNode(String path, boolean hasTimeExpression, boolean isMethod, List<ExpressionNode> parameterList, AccessorTypeEnum accessor) {
		this.path = path;
		this.hasTimeExpression = hasTimeExpression;
		this.isMethod = isMethod;
		this.parameterList = parameterList;
		this.accessor = accessor;
	}

	public void setAccessor(AccessorTypeEnum accessor) {
		this.accessor = accessor;
	}

	public AccessorTypeEnum getAccessor() {
		return accessor;
	}
	
	public String getPath() {
		return path;
	}

	public boolean hasTimeExpression() {
		return hasTimeExpression;
	}

	public boolean isMethod() {
		return isMethod;
	}

	public List<ExpressionNode> getParameterList() {
		if (parameterList == null) {
			parameterList = new LinkedList<ExpressionNode>();
		}
		return parameterList;
	}
	
	public String getValue(){
		if (this.isMethod()){
			return path+"(" + this.printParameters(getParameterList()) + ")";
		}
		if (path.equalsIgnoreCase("self")) {
			return "this";
		}
		if(this.hasTimeExpression){
			return path + "@pre";
		}
		return path;
	}
	
	private String printParameters(List<ExpressionNode> parameterList) {
		if(parameterList.size()==0){return"";}
		String result = "";
		for (ExpressionNode expressionNode : parameterList) {
			result+=expressionNode.getValue()+",";
		}
		return result.substring(0, result.length()-1);
	}

	public String toString() {
		return "[PropertyCallNode(path=" + path + ", hasTimeExpression=" + hasTimeExpression + ", isMethod=" + hasTimeExpression + ", parameterList=" + parameterList + ", accessor=" + accessor + ")]";
	}
}
